#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-11-8 14:14:58
# @Author : By Joker
# @Software: VS Code

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import itchat
import requests
import random


bot = None


def getNews():
    """
        获取金山词霸每日一句、英文和翻译
    """
    url = "http://open.iciba.com/dsapi/"
    req = requests.get(url)
    content = req.json()['content']
    note = req.json()['note']
    return content, note


def login_wechat():
    global bot
    bot = Bot()


def sendNews():
    if bot == None:
        login_wechat()

    try:
        content = getNews()
        print(content)

        friend = bot.friends().search('joke0m')[0]
        friend.send(content)

        t = Timer(60, sendNews)
        t.start()
    except:
        err = "发送消息失败"
        print(err)


def itchat_login_wx():
    itchat.login()
    itchat.send('hello ye','joke0rm')


if __name__ == "__main__":
    itchat_login_wx()
